package com.an.blog.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.an.blog.admin.domain.bo.BlogPhotoBo;
import com.an.blog.admin.domain.vo.BlogPhotoVo;
import com.an.blog.service.IBlogPhotoService;
import com.an.common.annotation.Log;
import com.an.common.annotation.RepeatSubmit;
import com.an.common.core.controller.BaseController;
import com.an.common.core.domain.PageQuery;
import com.an.common.core.domain.R;
import com.an.common.core.page.TableDataInfo;
import com.an.common.core.validate.AddGroup;
import com.an.common.core.validate.EditGroup;
import com.an.common.enums.BusinessType;
import com.an.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 相册图片
 *
 * @author Ann
 * @date 2023-05-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/blog/photo")
public class BlogPhotoController extends BaseController {

    private final IBlogPhotoService blogPhotoService;

    /**
     * 查询相册图片列表
     *
     * @param bo        业务对象
     * @param pageQuery 页面查询
     * @return {@link TableDataInfo }<{@link BlogPhotoVo }>
     * @author: Ann
     * @createDate: 2023/05/10 21:09:07
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:photo:list")
    @GetMapping("/list")
    public TableDataInfo<BlogPhotoVo> list(BlogPhotoBo bo, PageQuery pageQuery) {
        return blogPhotoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出相册图片列表
     *
     * @param bo       博
     * @param response 响应
     * @author Ann
     * @date 2024/05/14
     */
    @SaCheckPermission("blog:photo:export")
    @Log(title = "相册图片管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BlogPhotoBo bo, HttpServletResponse response) {
        List<BlogPhotoVo> list = blogPhotoService.queryList(bo);
        ExcelUtil.exportExcel(list, "相册图片", BlogPhotoVo.class, response);
    }

    /**
     * 获取相册图片详细信息
     *
     * @param photoId 主键
     * @return {@link R }<{@link BlogPhotoVo }>
     * @author Ann
     * @date 2024/05/14
     */
    @SaCheckPermission("blog:photo:query")
    @GetMapping("/{photoId}")
    public R<BlogPhotoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long photoId) {
        return R.ok(blogPhotoService.queryById(photoId));
    }

    /**
     * 新增相册图片
     *
     * @param bo 博
     * @return {@link R }<{@link Void }>
     * @author Ann
     * @date 2024/05/14
     */
    @SaCheckPermission("blog:photo:add")
    @Log(title = "相册图片管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BlogPhotoBo bo) {
        return toAjax(blogPhotoService.insertByBo(bo));
    }

    /**
     * 修改相册图片
     */
    @SaCheckPermission("blog:photo:edit")
    @Log(title = "相册图片管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BlogPhotoBo bo) {
        return toAjax(blogPhotoService.updateByBo(bo));
    }

    /**
     * 状态修改
     *
     * @param bo 业务对象
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/05/09 16:49:08
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:photo:edit")
    @Log(title = "相册图片管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody BlogPhotoBo bo) {
        return toAjax(blogPhotoService.updatePhotoStatus(bo));
    }

    /**
     * 删除相册图片
     *
     * @param photoIds 主键串
     */
    @SaCheckPermission("blog:photo:remove")
    @Log(title = "相册图片管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{photoIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] photoIds) {
        return toAjax(blogPhotoService.deleteWithValidByIds(Arrays.asList(photoIds), true));
    }
}
